package cn.xiaopengstack.domain.strategy.service.rule.tree.impl;

import cn.xiaopengstack.domain.strategy.model.entity.StrategyEntity;
import cn.xiaopengstack.domain.strategy.model.entity.UserEntity;
import cn.xiaopengstack.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import cn.xiaopengstack.domain.strategy.model.valobj.RuleTreeNodeVO;
import cn.xiaopengstack.domain.strategy.model.valobj.RuleTreeVO;
import cn.xiaopengstack.domain.strategy.service.rule.tree.ILogicTreeNode;
import cn.xiaopengstack.domain.strategy.service.rule.tree.engine.IDecisionTreeEngine;
import cn.xiaopengstack.domain.strategy.service.rule.tree.engine.impl.DecisionTreeEngine;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author jiangyangang
 * 规则树工厂
 */
@Component
public class DefaultTreeFactory {

    protected final Map<String, ILogicTreeNode> logicTreeNodeMap;

    public DefaultTreeFactory(Map<String, ILogicTreeNode> logicTreeNodeMap) {
        this.logicTreeNodeMap = logicTreeNodeMap;
    }

    public IDecisionTreeEngine process(RuleTreeVO ruleTreeVO){
        return new DecisionTreeEngine(logicTreeNodeMap, ruleTreeVO);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public static class TreeActionEntity{
        private RuleLogicCheckTypeVO ruleLogicCheckType;
        private StrategyAwardVO strategyAwardVo;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public static class TreeActionMatterEntity{
        /**
         * 用户信息
         */
        private UserEntity user;
        /**
         * 策略ID
         */
        private StrategyEntity strategy;
        /**
         * 奖品ID
         */
        private Integer awardId;

        /**
         * 规则树副本
         */
        private Map<String, RuleTreeNodeVO> ruleTreeMap;

        /**
         * 库存扣减数量
         */
        private Integer derNumber;
    }

    /**
     * 规则策略奖励详情数据
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public static class StrategyAwardVO {
        /** 抽奖奖品ID - 内部流转使用 */
        private Integer awardId;
        /** 抽奖奖品规则 */
        private String awardRuleValue;
    }
}
